﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;

public partial class News_GuestBook : System.Web.UI.Page
{
    const string cacheKey = "ordersTable";
    PM.PMSys.guestbook aguestbook = new PM.PMSys.guestbook();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["name"] != null && Session["usertype"] != null && Session["usertype"].ToString() != "1")
            {
                cbxOnlyUnpass.Visible = true;
                btnFilter.Visible = true;
            }
            DataListBind();
        }
    }
    DataTable loadFromXMLOrCache()
    {
        DataTable tbl = (DataTable)Cache[cacheKey];
        if (null == tbl)
        {
            string xmlFile = Server.MapPath("~/news/xmldb/xmldb.xml");
            tbl = new DataTable();
            tbl.ReadXml(xmlFile);
            CacheDependency dep = new CacheDependency(xmlFile);
            Cache.Insert(cacheKey, tbl, dep);
        }
        return tbl;
    }
    //XML数据抽取绑定到Datalist中
    protected void DataListBind()
    {
        PagedDataSource ps = new PagedDataSource();
        DataTable tbl = loadFromXMLOrCache();
        if (Session["name"] != null && Session["usertype"] != null && Session["usertype"].ToString() != "1")
        {
            if (cbxOnlyUnpass.Checked)
            {
                DataView dv = new DataView(tbl, "status='0'", null, DataViewRowState.CurrentRows);
                dv.Sort = "datetime desc";
                ps.DataSource = dv;
                ps.AllowPaging = true;
                ps.PageSize = 10;
                AspNetPager1.RecordCount = dv.Count;
                DataList1.DataSource = ps;
                DataList1.DataBind();
            } 
            else
            {
                DataView dv = tbl.DefaultView;
                dv.Sort = "datetime desc";
                ps.DataSource = dv;
                ps.AllowPaging = true;
                ps.PageSize = 10;
                AspNetPager1.RecordCount = dv.Count;
                DataList1.DataSource = ps;
                DataList1.DataBind();
            }
        }
        else
        {
            DataView dv = new DataView(tbl, "status='1'", null, DataViewRowState.CurrentRows);
            dv.Sort = "datetime desc";
            ps.DataSource = dv;
            ps.AllowPaging = true;
            ps.PageSize = 10;
            AspNetPager1.RecordCount = dv.Count;
            DataList1.DataSource = ps;
            DataList1.DataBind();
        }

    }

    //aspnetpager分页
    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        DataTable tbl = loadFromXMLOrCache();
        if (tbl != null && tbl.Rows.Count > 0)
        {
            if (Session["name"] != null && Session["usertype"] != null && Session["usertype"].ToString() != "1")
            {
                if (cbxOnlyUnpass.Checked)
                {
                    DataView dv = new DataView(tbl, "status='0'", null, DataViewRowState.CurrentRows);
                    dv.Sort = "datetime desc";
                    DataTable srcTbl = tbl.Clone();
                    for (int i = AspNetPager1.StartRecordIndex; i <= AspNetPager1.EndRecordIndex; i++)
                    {
                        srcTbl.ImportRow(dv[i - 1].Row);
                    }
                    DataList1.DataSource = srcTbl;
                    DataList1.DataBind();

                } 
                else
                {
                    DataView dv = tbl.DefaultView;
                    dv.Sort = "datetime desc";
                    DataTable srcTbl = tbl.Clone();
                    for (int i = AspNetPager1.StartRecordIndex; i <= AspNetPager1.EndRecordIndex; i++)
                    {
                        srcTbl.ImportRow(dv[i - 1].Row);
                    }
                    DataList1.DataSource = srcTbl;
                    DataList1.DataBind();

                }
            }
            else
            {
                DataView dv = new DataView(tbl, "status='1'", null, DataViewRowState.CurrentRows);
                dv.Sort = "datetime desc";
                DataTable srcTbl = tbl.Clone();
                for (int i = AspNetPager1.StartRecordIndex; i <= AspNetPager1.EndRecordIndex; i++)
                {
                    srcTbl.ImportRow(dv[i - 1].Row);
                }
                DataList1.DataSource = srcTbl;
                DataList1.DataBind();
            }
        }
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        string status=((Label)(e.Item.FindControl("lblstatus"))).Text;
        Label lblshowstatus = (Label)e.Item.FindControl("lblShowStatus");
        LinkButton lbreply=(LinkButton)e.Item.FindControl("btnreply");
        LinkButton lbdelete=(LinkButton)e.Item.FindControl("btndelete");
        Image Imgreply= (Image)e.Item.FindControl("imgreply");
        Image Imgdelete=(Image)e.Item.FindControl("imgdelete");
        if (status.Equals("1"))
        {
            lbreply.Text = "回复此留言";
        }
        else
        {
            lbreply.Text = "通过审核或回复此留言";
            lblshowstatus.Text = "该留言尚未审核";
        }
        if (Session["name"]!=null&&Session["usertype"]!=null&&Session["usertype"].ToString()!="1")
        {
               lbreply.Visible = true;
               Imgreply.Visible = true;
               lbdelete.Visible = true;
               Imgdelete.Visible = true;
        }
        else
        {
               lbreply.Visible = false;
               Imgreply.Visible = false;
               lbdelete.Visible = false;
               Imgdelete.Visible = false;
        }
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName=="Replyit")
        {
            Label lbl = new Label();
            lbl = (Label)e.Item.FindControl("lblname");
            Server.Transfer("ReplyComment.aspx?id="+e.CommandArgument.ToString()+"&name="+lbl.Text.ToString());
        }
        if (e.CommandName=="Deleteit")
        { 
            bool result=aguestbook.DeleteComment(e.CommandArgument.ToString());
            if (result)
            {
                PM.Code.Web.alertmessage.ShowMsg("浏览删除成功!", "GuestBook.aspx");
            }
        }
    }
    protected void btnFilter_Click(object sender, EventArgs e)
    {
        DataListBind();
    }
}
